$(document).ready(function() { 
	$('.edit').click(function(){
		var parent = $(this).parent();
		var trainProgramme = $(this).parent().parent().find(".trainProgramme");
		var edit  = $(this);
		edit.toggleClass('hide');
		trainProgramme.data("last",encode(trainProgramme.html()))
		trainProgramme.css("display", 'none');
		trainProgramme.after('<textarea name="content" class="trainProgrammeContent">'+ trainProgramme.data("last") +'</textarea>');
		$(this).before('<a href="javascript:void(0);" class="trainProgrammeSave" >保存</a>');
		$(this).before('<a href="javascript:void(0);" class="trainProgrammeCancel" >取消</a>');
//		取消
		$('.trainProgrammeCancel').click(function(){
		  
						trainProgramme.html(decode(trainProgramme.data("last")));
						edit.toggleClass('hide');
						trainProgramme.css("display", 'block');
						$('.trainProgrammeContent').remove();
						$('.trainProgrammeSave').remove();
						$('.trainProgrammeCancel').remove(); 
		});

//		提交编辑
		$('.trainProgrammeSave').click(function(){
			var data = {};
			$.each(parent.find(".datazone"), function(index, value) { 
				data[value.name] = value.value; 
			});
			data['description'] = decode($('.trainProgrammeContent').val());
			  
			$.post("/cloud/trainProgramme/update", data , 
				function(json){
					if (json.success)
					{ 
						trainProgramme.html(decode($('.trainProgrammeContent').val()));
						edit.toggleClass('hide');
						trainProgramme.css("display", 'block');
						$('.trainProgrammeContent').remove();
						$('.trainProgrammeSave').remove();
						$('.trainProgrammeCancel').remove(); 
					}
				}, 
				'json');  
		});
	}) ;
});
 
function encode(sourec){
	sourec = sourec.replace(/\<br\>/gi,"\n");
	return sourec;
} 

function decode(target){
	return target.replace(/\n/g,"<br>");
} 